package com.lizicloud.infrastructure.common.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期时间工具类
 */
public class DateTimeUtils {
    
    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    
    private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern(DEFAULT_FORMAT);
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT);
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_FORMAT);
    
    /**
     * 获取当前时间字符串
     */
    public static String getCurrentDateTimeString() {
        return LocalDateTime.now().format(DEFAULT_FORMATTER);
    }
    
    /**
     * 获取当前日期字符串
     */
    public static String getCurrentDateString() {
        return LocalDateTime.now().format(DATE_FORMATTER);
    }
    
    /**
     * 获取当前时间字符串
     */
    public static String getCurrentTimeString() {
        return LocalDateTime.now().format(TIME_FORMATTER);
    }
    
    /**
     * 格式化LocalDateTime
     */
    public static String formatDateTime(LocalDateTime dateTime) {
        if (dateTime == null) {
            return null;
        }
        return dateTime.format(DEFAULT_FORMATTER);
    }
    
    /**
     * 解析字符串为LocalDateTime
     */
    public static LocalDateTime parseDateTime(String dateTimeString) {
        if (dateTimeString == null || dateTimeString.trim().isEmpty()) {
            return null;
        }
        try {
            return LocalDateTime.parse(dateTimeString, DEFAULT_FORMATTER);
        } catch (Exception e) {
            return null;
        }
    }
    
    private DateTimeUtils() {
        // 私有构造函数，防止实例化
    }
}